package com.tzpay.tzpay.common;

import com.tzpay.tzpay.api.NormalCallback;
import java.lang.ref.WeakReference;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by chenfeiyue on 16/3/18.
 */
public abstract class BaseCallBack<T, V> extends NormalCallback<V> {
    private WeakReference<T> ref;

    /**
     * 构造方法。
     *
     * @param t 所在的对象。
     */
    public BaseCallBack(T t) {
        ref = new WeakReference<T>(t);
    }

    @Override
    public void onResponse(Call<V> call, Response<V> response) {
        super.onResponse(call, response);
        if (ref == null) {
            return;
        }
        T t = ref.get();
        if (t == null) {
            return;
        }
        onResponseEx(t, call, response);
    }

    @Override
    public void onFailure(Call<V> call, Throwable throwable) {
        super.onFailure(call, throwable);
        if (ref == null) {
            return;
        }
        T t = ref.get();
        if (t == null) {
            return;
        }
        if (throwable != null)
            throwable.printStackTrace();
        onFailureEx(t, call, throwable);
    }

    public abstract void onResponseEx(T t, Call<V> call, Response<V> response);

    public abstract void onFailureEx(T t, Call<V> call, Throwable throwable);
}
